<?php
	class Main extends CI_Controller{
			//分页显示数据
		public function page_list(){
			//加载时区
			date_default_timezone_set('Asia/Shanghai');
				//加载模型
			$this->load->model("Mainmodel","usermodel");
			$perpage=5;
			if (intval($this->uri->segment(3)) > 0) {
				$prevpage = intval($this->uri->segment(3)) - 1;
			} else {
				$prevpage = intval($this->uri->segment(3));
			}
			$data["data"] = $this->usermodel->page_list($prevpage * $perpage,$perpage);
			// var_dump($result);

			//下面开始分页
			//加载类库
			 $this->load->library('pagination');
	        // $perPage=10;//每页10条
	        //配置项设置
	        $config['base_url']=site_url("Main/page_list/");
	        $config['total_rows']=$this->db->count_all_results('user');
	        $config['per_page'] = $perpage;

			$config['use_page_numbers'] = TRUE;

			$config['full_tag_open'] = '<ul class="page">';
			$config['full_tag_close'] = '</ul>';

			$config['first_link'] = "<li style='color:white;'>首页</li>";
			$config['last_link'] = "<li style='color:white;'>尾页</li>";
        	$config['prev_link']="上一页";
        	$config['next_link']="下一页";
			
			// $config['prev_link'] = '&lt;&lt;';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';

			// $config['next_link'] = '&gt;&gt;';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';

			$config['cur_tag_open'] = "<li class='active'>";
			$config['cur_tag_close'] = '</li>';

			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';

			$this->pagination->initialize($config);

			$data["links"] = $this->pagination->create_links();
			//加载的视图文件
			$this->load->view("home/user_list",$data);
		}


		//获取数据并显示到视图
		public function user_list($data){
			$this->load->database();
			date_default_timezone_set('Asia/Shanghai');
			// $this->load->model("Mainmodel");
			// $data["data"] = $this->Mainmodel->get_user();
			//使用view传递数据 必须使用$data["name"]方式，在视图文件中用$name 输出数据 
			$this->load->view('home/user_list',$data);
		}
		//增加
		public function add(){			
			$this->load->view('home/add');
		}
		//编辑
		public function edit(){
			$this->load->view('home/edit');
		}
		//用户单个查询
		public function user_edit(){
			$this->load->database(); 
			$this->load->model("Mainmodel");
			$id = $_GET['id'];
			$data['data'] = $this->Mainmodel->row_id($id);
			$this->load->view('home/edit',$data);
		}
		//用户模糊查询
		public function user_query(){
			$this->load->database();
			date_default_timezone_set('Asia/Shanghai');
			$this->load->model("Mainmodel");
			$queryname = $_POST['queryname'];
			$data["data"] = $this->Mainmodel->vague_user($queryname);
			if($data["data"]){
				$this->load->view('home/user_list',$data);	
			}else{
				echo "请输入查询的关键字";
			}
		}

		//用户编辑修改
		public function user_save(){
			$this->load->database();
			$this->load->model("Mainmodel");
			$data["editid"] = $this->input->post("editid");
			$data["editname"] = $this->input->post("editname");
			$data["editpassword"] = $this->input->post("editpassword");
		    $data["editrorle"] = $this->input->post("editrorle");
		    if($this->input->post("editstate")==NULL){
		            	$data["editstate"]=0;
		            }else{
		            	$data["editstate"] = 1;
		            }
		    if(!$row_user = $this->Mainmodel->row_user($data["editname"])){

		    }
		    $datas = array("username"=>$data["editname"],"userpassword"=>$data["editpassword"],"userorle"=>$data["editrorle"],"userstate"=>$data["editstate"]);
			$bool=$this->db->update('user',$datas,array('id'=>$data["editid"]));
			if($bool){
				echo "修改成功,请关闭窗口";
			}else{
				echo "修改失败";
			}
		}

		//用户删除
		public function user_dele(){
			// $id = (int)input('post.id');
			$id = $_GET['id'];
			$this->load->database();
			$this->load->model("Mainmodel");
			$res = $this->Mainmodel->dele_user($id);
			$this->page_list();
		}
			
		//用户添加
		public function user_add(){
			$this->load->model("Mainmodel");
			$this->load->helper(array('form', 'url'));
        	$this->load->library('form_validation');
        	$this->form_validation->set_rules('username', '用户名', 'required|max_length[16]');
			$this->form_validation->set_rules('userpassword', '密码', 'required|max_length[16]');
			$this->form_validation->set_rules('userorle', '角色', 'required');
			if ($this->form_validation->run() == FALSE){
		            // $this->load->view('myform');
		            echo "error";
		            redirect("main/add");
		    } else {
		            $data["username"] = $this->input->post("username");
		            $data["userpassword"] = $this->input->post("userpassword");
		            $data["userorle"] = $this->input->post("userorle");
		            if($this->input->post("userstate")==NULL){
		            	$data["userstate"]=0;
		            }else{
		            	$data["userstate"] = $this->input->post("userstate");
		            }
		            // $data["time"] = date("Y-m-d H:i:s",time());
		            $data["time"] = time();
		            //判断用户名不能相同
		            if(!$row_user = $this->Mainmodel->row_user($data["username"])){
		            	if ($res = $this->Mainmodel->add_user($data)) {
				            	# 注册成功
				            	// echo "<script>layer.closeAll()</script>";
				 			echo "添加成功,请关闭窗口";			         		
				            }else{
				            	echo "error";
				            }
		            }else{
		            	echo "用户名已存在";
		            }	            
		    }
		}
		//
						
}
?>